﻿#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>  
#include <math.h>  

#define identifying_code "114514" 

void _9x9()
{
    for (int i = 1; i < 10; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            printf("%d x %d =%2d  ", j, i, i * j);
        }
        printf("\n");
    }
}
void isprime()
{
    char flag = 0;
    printf("请输入一个正整数：");
    unsigned int n;
    scanf("%u", &n);
    if (n == 2 || n == 3 || n == 5)
        flag = 1;
    else if (n % 2 == 0 || n % 3 == 0 || n % 5 == 0)
    {
        flag = 0;
    }
    else {
        for (int i = 5; i < sqrt(n); i += 30)
        {
            if (n % i == 0)
                flag = 0;
        }
        flag = 1;
    }
    printf("%u%s质数", n, (flag) ? "是" : "不是");
}

char choice;

const char* permission[] = { "读权限","写权限","执行权限","删除权限" };
const char* function[] = { "九九乘法表","质数判断"};

void judge(unsigned int k)
{
    int i;
    int found = 0;

    if (k == 0)
    {
        printf("无任何权限!请返回添加权限！\n");
        return;
    }
    else {
        printf("当前权限: ");
        for (i = 0; i < 4; i++)
        {
            if (k & (1 << i)) 
            {
                printf("%s ", permission[i]);
                found = 1;
            }
        }
        if (!found) {
            printf("无任何权限!");
        }
        printf("\n");
    }
}
void addpermission(unsigned int* k)
{
    printf("请选择需要添加的权限,以空格分隔,输入完成后按回车结束\n1.读权限\n2.写权限\n3.执行权限\n4.删除权限\n");
    char input[100];
    getchar();  
    fgets(input, sizeof(input), stdin); 

    char* token = strtok(input, " \n");
    while (token != NULL)
    {
        switch (token[0])
        {
        case '1':
            if (*k & 1) 
                printf("已有读权限！\n");
            else {
                *k |= 1;
                printf("已添加读权限\n");
            }
            break;
        case '2':
            if (*k & 2) 
                printf("已有写权限！\n");
            else {
                *k |= 2;
                printf("已添加写权限\n");
            }
            break;
        case '3':
            if (*k & 4)
                printf("已有执行权限！\n");
            else {
                *k |= 4;
                printf("已添加执行权限\n");
            }
            break;
        case '4':
            if (*k & 8) 
                printf("已有删除权限！\n");
            else {
                *k |= 8;
                printf("已添加删除权限\n");
            }
            break;
        default:
            printf("无效选择: %c\n", token[0]);
            break;
        }
        token = strtok(NULL, " \n");
    }
}
void excute()
{
    printf("1.九九乘法表 ");
    _9x9();
    printf("2.质数判断");
    isprime();
}


int main()
{
    unsigned int right = 0;

    printf("=== 权限管理系统 ===\n");
    printf("初始权限状态: ");
    judge(right);
    printf("请输入校验码: ");
    char str[10];
    scanf("%s", str);
    if (strcmp(str, identifying_code) == 0)  
    {
        printf("校验成功！\n");
        addpermission(&right);
    }
    else
    {
        printf("校验码错误！\n");
    }
    judge(right);  
    if (right & 4)
    {
        printf("当前已有执行权限，是否执行预定功能？1.是2.否");
        scanf("%c", &choice);
        if (choice == '1')
        {
            excute();
        }
        else {
            return 0;
        }
       
    }
    return 0;
}